
package com.chongchong.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chongchong.common.tool.EmailCommonUtil;
import com.chongchong.core.boot.ctrl.SysController;
import com.chongchong.core.mp.support.Condition;
import com.chongchong.core.mp.support.Query;
import com.chongchong.core.tool.api.R;
import com.chongchong.core.tool.utils.Func;
import com.chongchong.system.dto.EmailTemplateDTO;
import com.chongchong.system.entity.EmailTemplate;
import com.chongchong.system.service.IEmailTemplateService;
import com.chongchong.system.vo.EmailTemplateVO;
import com.chongchong.system.wrapper.EmailWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 *  控制器
 *
 * @author en
 * @since 2022-07-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("/email")
@Api(value = "邮箱模板接口", tags = "邮箱模板接口")
public class EmailTemplateController extends SysController {

	private IEmailTemplateService emailTemplateService;

	/**
	* 详情
	*/
	@GetMapping("/detail")
    @ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sysEmailTemplate")
	public R<EmailTemplateVO> detail(EmailTemplate emailTemplate) {
		EmailTemplate detail = emailTemplateService.getOne(Condition.getQueryWrapper(emailTemplate));
		return R.data(EmailWrapper.build().entityVO(detail));
	}

	/**
	* 分页
	*/
	@GetMapping("/list")
    @ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sysEmailTemplate")
	public R<IPage<EmailTemplateVO>> list(EmailTemplateDTO emailTemplate, Query query) {
		IPage<EmailTemplateVO> pages = emailTemplateService.selectSysEmailTemplatePage(Condition.getPage(query), emailTemplate);
		return R.data(pages);
	}

//	/**
//	* 自定义分页
//	*/
//	@GetMapping("/page")
//    @ApiOperationSupport(order = 3)
//	@ApiOperation(value = "分页", notes = "传入sysEmailTemplate")
//	public R<IPage<EmailTemplateVO>> page(EmailTemplateVO sysEmailTemplate, Query query) {
//		IPage<EmailTemplateVO> pages = emailTemplateService.selectSysEmailTemplatePage(Condition.getPage(query), sysEmailTemplate);
//		return R.data(pages);
//	}

	/**
	* 新增
	*/
	@PostMapping("/save")
    @ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入sysEmailTemplate")
	public R save(@Valid @RequestBody EmailTemplate emailTemplate){
		return emailTemplateService.saveData(emailTemplate);
	}

	/**
	* 修改
	*/
	@PostMapping("/update")
    @ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入sysEmailTemplate")
	public R update(@Valid @RequestBody EmailTemplate emailTemplate) {
		return emailTemplateService.updateData(emailTemplate);
	}

	/**
	* 新增或修改
	*/
	@PostMapping("/submit")
    @ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入sysEmailTemplate")
	public R submit(@Valid @RequestBody EmailTemplate emailTemplate) {
		return R.status(emailTemplateService.saveOrUpdate(emailTemplate));
	}


	/**
	* 删除
	*/
	@PostMapping("/remove")
    @ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(emailTemplateService.removeByIds(Func.toLongList(ids)));
	}
    @Autowired
	EmailCommonUtil emailCommonUtil;
	/**
	 * 发送邮件
	 */
	@PostMapping("/sendEmail")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "发送邮件", notes = "发送邮件")
	public R sendEmail(@ApiParam(value = "邮件地址", required = true) @RequestParam String emailAdress,
					   @ApiParam(value = "邮件标题", required = true) @RequestParam String title,
					   @ApiParam(value = "邮件主题", required = true) @RequestParam String theme,
					   @ApiParam(value = "邮件内容", required = true) @RequestParam String content) {
		emailCommonUtil.sendMail(Func.toStrList(";",emailAdress),theme,title,content);
		return R.status(true);
	}


	@GetMapping("/listAll")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "查询所有", notes = "可根据模板名称模糊搜索")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "mouldName", value = "mouldName", paramType = "query", dataType = "string" )
	})
	public R<List<EmailTemplateVO>> listAll(String mouldName) {
		List<EmailTemplateVO> list = emailTemplateService.listAll(mouldName);
		return R.data(list);
	}

	@GetMapping("/createMouldNo")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "自动生成模板编号")
	public R<String> createMouldNo() {
		String mouldNo = emailTemplateService.createEmailMouldNo();
		return R.data(mouldNo);
	}

}
